
#include <system.h>
#include <osa_cmem.h>
#include <mcvip.h>

int SYSTEM_init(int debug,int numMCVIP,int audioChan)
{
	int status;
	int devAddr[MCVIP_VIDEO_INPUT_PORT_MAX * MCVIP_TVP5158_MAX_CASCADE];
	int audioDacAddr;

	status = CMEM_init();
	if(status < 0) 
	{
		OSA_ERROR("CMEM_init()\n");
		return status;
	}

	status = DRV_dmaInit();
	if(status<0) 
	{
		OSA_ERROR(" DRV: ERROR: DRV_dmaInit()\n");
		return CMEM_exit();
	}

	memset(devAddr, 0, sizeof(devAddr));
	audioDacAddr = 0;

	if(!debug) 
	{  
		// control TVP5158 I2C via software 
		if(numMCVIP == 2)
		{
			devAddr[0] = TVP5158_A_I2C_ADDR;
			devAddr[1] = TVP5158_B_I2C_ADDR;  
		}
		else
		{
			devAddr[0] = TVP5158_A_I2C_ADDR;
			devAddr[1] = TVP5158_I2C_ADDR_NONE;  
		}
		audioDacAddr = TLV320DAC3x_A_I2C_ADDR;
	} 
	else 
	{
		// debug mode, let WinVCC tool control TVP5158 I2C
	}

	status = MCVIP_init(devAddr,audioDacAddr);
	if(status < 0) 
	{
		OSA_ERROR("MCVIP_init()\n");
		CMEM_exit();
		return status;
	}

	return status;
}

int SYSTEM_exit()
{
	MCVIP_exit();
	printf("MCVIP_exit\n");
	CMEM_exit();
	printf("CMEM_exit\n");
	DRV_dmaExit();
	printf("DRV_dmaExit\n");

	return OSA_SOK;
}

char SYSTEM_getInput()
{
  return getchar();
}
